package smartcounters.androidclient;

import java.util.EnumSet;
import java.util.LinkedList;

import smartcounters.androidclient.WebServices.InformationUpdaterWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.eOperationsEnum;
import smartcounters.androidclient.model.eUserPermissions;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class UserDetailsActivity extends Activity implements OnClickListener, OnResultReceiver{
	private String[] m_permissionsArray;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_details_layout);
        
        Button updateUserButton = (Button)findViewById(R.id.buttonUpdateUser);
        updateUserButton.setOnClickListener(this);

        Button deleteUserButton = (Button)findViewById(R.id.buttonDeleteUser);
        deleteUserButton.setOnClickListener(this);
        
		Spinner permissionsSpinner = (Spinner)findViewById(R.id.userDetailsPermissionsSpinner);
        
        m_permissionsArray = new String[2];
        m_permissionsArray[0] = "Administrator";
        m_permissionsArray[1] = "User";
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_permissionsArray);
        permissionsSpinner.setAdapter(adapter);
        
        EditText userIDEditText = (EditText)findViewById(R.id.userDetailsIDEditText);
        EditText updateUserUsernameEditText = (EditText)findViewById(R.id.userDetailsUsernameEditText);
		EditText userPhoneEditText = (EditText)findViewById(R.id.userDetailsUserPhoneEditText);
		EditText userMobilePhoneEditText = (EditText)findViewById(R.id.userDetailsUserMobilePhoneEditText);
		EditText emailEditText = (EditText)findViewById(R.id.userDetailsEmailEditText);
		EditText userPasswordEditText = (EditText)findViewById(R.id.userDetailsUserPasswordEditText);
		EditText accountsAssociatedWithEditText = (EditText)findViewById(R.id.userDetailsAccountsAssociatedWithEditText);
		
		userIDEditText.setText(String.valueOf(FindUserResultsActivity.s_User.getID()));
		updateUserUsernameEditText.setText(FindUserResultsActivity.s_User.getUsername());
		userPhoneEditText.setText(FindUserResultsActivity.s_User.getPhone());
		userMobilePhoneEditText.setText(FindUserResultsActivity.s_User.getMobilePhone());
		emailEditText.setText(FindUserResultsActivity.s_User.getEmail());
		userPasswordEditText.setText(FindUserResultsActivity.s_User.getPassword());
		
		String accountsAssociatedWith = "";
		LinkedList<Integer> accounts = FindUserResultsActivity.s_User.getAccountsAssociatedWith();
		for (int account : accounts){
			accountsAssociatedWith += String.valueOf(account) + ",";
		}
		
		if (accountsAssociatedWith.length() > 0){
			accountsAssociatedWith = accountsAssociatedWith.substring(0, accountsAssociatedWith.length() - 1);
		}
		
		accountsAssociatedWithEditText.setText(accountsAssociatedWith);
		
		if (FindUserResultsActivity.s_User.getUserPermissions().contains(eUserPermissions.All)){
			permissionsSpinner.setSelection(0);
		}
		else{
			permissionsSpinner.setSelection(1);
		}
    }

	public void onClick(View v) {
		if (v.getId() == R.id.buttonUpdateUser){
			EditText userIDEditText = (EditText)findViewById(R.id.userDetailsIDEditText);
			EditText updateUserUsernameEditText = (EditText)findViewById(R.id.userDetailsUsernameEditText);
			EditText userPhoneEditText = (EditText)findViewById(R.id.userDetailsUserPhoneEditText);
			EditText userMobilePhoneEditText = (EditText)findViewById(R.id.userDetailsUserMobilePhoneEditText);
			EditText emailEditText = (EditText)findViewById(R.id.userDetailsEmailEditText);
			EditText userPasswordEditText = (EditText)findViewById(R.id.userDetailsUserPasswordEditText);
			EditText accountsAssociatedWithEditText = (EditText)findViewById(R.id.userDetailsAccountsAssociatedWithEditText);
			Spinner permissionsSpinner = (Spinner)findViewById(R.id.userDetailsPermissionsSpinner);
			Button updateUserButton = (Button)findViewById(R.id.buttonUpdateUser);
	        Button deleteUserButton = (Button)findViewById(R.id.buttonDeleteUser);
			
	        int[] accounts = null;
			String[] accountsStrings = accountsAssociatedWithEditText.getText().toString().split(",");
			if (accountsStrings.length == 1 && accountsStrings[0].length() == 0)
			{
				accounts = new int[0];
			}
			else
			{
				accounts = new int[accountsStrings.length];
				for (int i = 0; i < accountsStrings.length; i++){
					accounts[i] = Integer.parseInt(accountsStrings[i]);
				}
			}
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			EnumSet<eUserPermissions> permissions;
			
			if (((String)permissionsSpinner.getSelectedItem()) == "Administrator"){
				permissions = EnumSet.of(eUserPermissions.All);
			}
			else{
				permissions = EnumSet.of(
						eUserPermissions.CreateFault,
						eUserPermissions.EditAssociatedAccountInfo,
						eUserPermissions.EditOwnUserInfo,
						eUserPermissions.ReadAssociatedAccountInfo,
						eUserPermissions.ReadOwnBill,
						eUserPermissions.ReadOwnUserInfo,
						eUserPermissions.UpdateUsage);
			}
			
			updateUserUsernameEditText.setEnabled(false);
			permissionsSpinner.setEnabled(false);
			userPhoneEditText.setEnabled(false);
			userMobilePhoneEditText.setEnabled(false);
			emailEditText.setEnabled(false);
			userPasswordEditText.setEnabled(false);
			accountsAssociatedWithEditText.setEnabled(false);
			updateUserButton.setEnabled(false);
			deleteUserButton.setEnabled(false);
			
			updaterClient.RegisterResultReceiver(this);
			
			TextView messageTextView = (TextView)findViewById(R.id.userDetailsMessageTextView);
			messageTextView.setText("Please wait...");
			
			updaterClient.UpdateUser(
					Configuration.getInstance().getUserCredentials(),
					Integer.parseInt(userIDEditText.getText().toString()),
					updateUserUsernameEditText.getText().toString(),
					permissions,
					userPhoneEditText.getText().toString(),
					userMobilePhoneEditText.getText().toString(),
					emailEditText.getText().toString(),
					userPasswordEditText.getText().toString(),
					accounts);
		}
		else if (v.getId() == R.id.buttonDeleteUser){
			EditText userIDEditText = (EditText)findViewById(R.id.userDetailsIDEditText);
			
			TextView messageTextView = (TextView)findViewById(R.id.userDetailsMessageTextView);
			messageTextView.setText("Please wait...");
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			updaterClient.RegisterResultReceiver(this);
			
			updaterClient.DeleteUser(Configuration.getInstance().getUserCredentials(), Integer.parseInt(userIDEditText.getText().toString()));
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText updateUserUsernameEditText = (EditText)findViewById(R.id.userDetailsUsernameEditText);
		EditText userPhoneEditText = (EditText)findViewById(R.id.userDetailsUserPhoneEditText);
		EditText userMobilePhoneEditText = (EditText)findViewById(R.id.userDetailsUserMobilePhoneEditText);
		EditText emailEditText = (EditText)findViewById(R.id.userDetailsEmailEditText);
		EditText userPasswordEditText = (EditText)findViewById(R.id.userDetailsUserPasswordEditText);
		EditText accountsAssociatedWithEditText = (EditText)findViewById(R.id.userDetailsAccountsAssociatedWithEditText);
		Spinner permissionsSpinner = (Spinner)findViewById(R.id.userDetailsPermissionsSpinner);
		Button updateUserButton = (Button)findViewById(R.id.buttonUpdateUser);
        Button deleteUserButton = (Button)findViewById(R.id.buttonDeleteUser);
		
        if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.UpdateUser){
        	TextView messageTextView = (TextView)findViewById(R.id.userDetailsMessageTextView);
			messageTextView.setText("The user was updated.");
        }
        else if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.DeleteUser){
        	TextView messageTextView = (TextView)findViewById(R.id.userDetailsMessageTextView);
			messageTextView.setText("The user was deleted.");
			
			Intent intent = new Intent();
			intent.setClass(this, MainActivity.class);
			startActivity(intent);
        }
        
        updateUserUsernameEditText.setEnabled(true);
		permissionsSpinner.setEnabled(true);
		userPhoneEditText.setEnabled(true);
		userMobilePhoneEditText.setEnabled(true);
		emailEditText.setEnabled(true);
		userPasswordEditText.setEnabled(true);
		accountsAssociatedWithEditText.setEnabled(true);
		updateUserButton.setEnabled(true);
		deleteUserButton.setEnabled(true);
	}
}